
#include "face_window.h"

FaceWindow::FaceWindow(const int width, const int height, const string title, QWidget *parent)
    : QMainWindow(parent)
{
    /// Appearance
    int x, y;
    centerWindow(width, height, x, y);  // move to center
    resize(width, height);
    move(x, y);

    appTitle = title;
    setWindowTitle(appTitle.c_str());   // set title
    setWindowIcon(QIcon(DEFAULT_ICON.c_str()));    // set icon

    setupMenus();
    setupStatusBar();
    setupLayouts();
}

void FaceWindow::centerWindow(const int w, const int h, int &x, int &y)
{
    int screenWidth;
    int screenHeight;

    QDesktopWidget *desktop = QApplication::desktop();

    screenWidth = desktop->width();
    screenHeight = desktop->height();

    x = (screenWidth - w) >> 1;
    y = (screenHeight - h) >> 1;
}

void FaceWindow::setupMenus()
{
    /// Menus
    QMenu *fileMenu = menuBar()->addMenu("&File");
    QAction *quit = new QAction("&Quit", this);
    fileMenu->addAction(quit);
    connect(quit, SIGNAL(triggered()), qApp, SLOT(quit()));

    QMenu *toolsMenu = menuBar()->addMenu("&Tools");
    actionCamera = new QAction("&Camera", this);
    actionCamera->setCheckable(true);
    actionCamera->setChecked(false);
    toolsMenu->addAction(actionCamera);
    connect(actionCamera, SIGNAL(triggered()), this, SLOT(toggleCamera()));
    actionAlignment = new QAction("&Alignment", this);
    actionAlignment->setCheckable(true);
    actionAlignment->setChecked(false);
    toolsMenu->addAction(actionAlignment);
    connect(actionAlignment, SIGNAL(triggered()), this, SLOT(toggleAlignment()));

    QMenu *helpMenu = menuBar()->addMenu("&Help");
    QAction *help = new QAction("&Help", this);
    QAction *about = new QAction("&About", this);
    helpMenu->addAction(help);
    helpMenu->addAction(about);
}

void FaceWindow::setupLayouts()
{
    fa = new FaceAlignment(this);
    setCentralWidget(fa);
}

void FaceWindow::setupStatusBar()
{
    /// Status bars
    statusBar()->showMessage("Ready");
}

void FaceWindow::toggleCamera()
{
    if (actionCamera->isChecked())
    {
        onOpenCamera();
    }
    else 
    {
        onCloseCamera();
    }
}

void FaceWindow::onOpenCamera()
{
    fa->openCamera();
}

void FaceWindow::onCloseCamera()
{
    fa->closeCamera();
}

void FaceWindow::toggleAlignment()
{
    if (actionAlignment->isChecked())
    {
        onStartAlignment();
    }
    else 
    {
        onStopAlignment();
    }
}

void FaceWindow::onStartAlignment()
{
    fa->setDoAlign(true);
}

void FaceWindow::onStopAlignment()
{
    fa->setDoAlign(false);
}

